package com.netrust.cmsx.core.persist;


import java.io.Serializable;

import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.Table;

import org.springframework.data.jpa.repository.support.SimpleJpaRepository;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

@Repository
@Transactional(readOnly = true)
public class BaseDaoImpl<T,ID extends Serializable>  extends SimpleJpaRepository<T, ID> implements BaseDao<T,ID> {
	protected final EntityManager em;
	 
	 //父类没有不带参数的构造方法，这里手动构造父类
    public BaseDaoImpl(Class<T> domainClass, EntityManager entityManager) {
        super(domainClass, entityManager);
        this.em = entityManager;
    }

	public void testRun() {
		System.out.println("------------------testRun--------------------------");
	}
	
	
	
	 /** 
     * 获取实体的名称 
     * @param <T> 
     * @param entityClass 实体类 
     * @return 
     */  
    @SuppressWarnings("hiding")
	protected <T> String getEntityName(Class<T> entityClass) {  
        String entityname = entityClass.getSimpleName();  
        Entity entity = entityClass.getAnnotation(Entity.class);  
        if(entity.name() != null && !"".equals(entity.name())) {  
            entityname = entity.name();  
        }  
        return entityname;  
    } 
    
	 /** 
     * 获取table的名称 
     * @param <T> 
     * @param entityClass 实体类 
     * @return 
     */  
    @SuppressWarnings("hiding")
	protected <T> String getTableName(Class<T> entityClass) {  
        String entityname = entityClass.getSimpleName();  
        Table table = entityClass.getAnnotation(Table.class);  
        if(table.name() != null && !"".equals(table.name())) {  
            entityname = table.name();  
        }  
        return entityname;  
    }  
}